﻿#pragma once

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cassert>
using namespace std;
namespace ljt
{
	template<typename T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		// 构造函数，初始化列表  vector<int> v;
		vector()
		{}

		// 拷贝构造函数 vector<int> v1(v);
		// = 构造函数 + 赋值运算符重载函数
		// vector<int> v, v1;     v1 = v;

		// 拷贝构造函数 vector<int> v1(v);
		/*vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto& e : v)
				push_back(e);
		}*/

		vector(const vector<T>& v)
		{
			_start = new T[v.capacity()];
			//memcpy(_start, v._start, sizeof(T) * v.size());
			for (size_t i = 0; i < v.size(); ++i)
			{
				_start[i] = v._start[i];
			}
			_finish = _start + v.size();
			_end_of_storage = _start + v.capacity();
		}

		vector(size_t n, const T& val = T())
		{
			resize(n, val);
		}

		vector(int n, const T& val = T())
		{
			resize(n, val);
		}

		template<typename InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		~vector()
		{
			if (_start)
			{
				delete[]_start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		void reserve(size_t n)
		{
			if (capacity() < n)
			{
				// 避免迭代器失效
				size_t len = size();
				T* tmp = new T[n];
				if (_start)
				{
					//memcpy(tmp, _start, len * sizeof(T));
					for (size_t i = 0; i < v.size(); ++i)
					{
						_start[i] = v._start[i];
					}
					delete[] _start;
				}

				_start = tmp;
				_finish = _start + len;
				_end_of_storage = _start + n;
			}
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}

		// 现代写法  Copy-and-Swap
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		// 深拷贝，参数是自定义类型，先经过初始化列表
		// 返回值是&，因为可支持连续赋值
		// 赋值运算符重载
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		// 当 insert触发扩容（reserve）时，原有内存会被释放，导致外部持有的迭代器（如 v.begin()）变成野指针。
		// 返回新迭代器​​：告知调用者插入元素后的有效位置，避免后续操作访问失效迭代器
		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);

			if (_finish == _end_of_storage)
			{
				// 避免迭代器失效，指向原内存[被销毁]
				size_t offset = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + offset;
			}

			auto end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}
			*pos = x;
			++_finish;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);

			auto it = pos + 1;
			while (it != _finish)
			{
				*(it - 1) = *it;
				++it;
			}
			--_finish;

			return pos;
		}

		void push_back(const T& x)
		{
			// 扩容
			/*if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}

			*_finish = x;
			++_finish;*/

			insert(end(), x);
		}

		void pop_back()
		{
			erase(end() - 1);
		}

		void resize(size_t n, const T& val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}

			else {
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};
}